<template>
  <div class="login-container">
    <div class="login-content">
      <div class="logo-section">
        <div class="logo">📱 抖音</div>
        <div class="tagline">记录美好生活</div>
      </div>

      <div class="form-section">
        <div class="form-group">
          <input v-model="form.username" type="text" placeholder="请输入用户名/邮箱" class="input-field"
            @keypress.enter="loginAction">
        </div>

        <div class="form-group password-group">
          <input v-model="form.password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码"
            class="input-field" @keypress.enter="loginAction">
          <span class="password-toggle" @click="showPassword = !showPassword">
            {{ showPassword ? '🙈' : '👁️' }}
          </span>
        </div>

        <button @click="loginAction" :disabled="loading" class="login-btn">
          {{ loading ? '登录中...' : '登录' }}
        </button>

        <div class="register-link">
          还没有账号？
          <span @click="$router.push('/register')" class="link-text">
            注册
          </span>
        </div>
      </div>
    </div>

    <div class="footer">
      <p>登录即表示同意抖音的服务条款</p>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      showPassword: false,
      loading: false
    }
  },
  created() {
    // 如果已经登录，直接跳转到首页
    if (this.$store.getters.isLoggedIn) {
      // 如果已经有重定向目标，使用该目标，否则去首页
      const redirectPath = this.$route.query.redirect || '/feed'
      this.$router.push(redirectPath)
    }
  },
  methods: {
    ...mapActions(['login']),

    async loginAction() {
      if (!this.form.username || !this.form.password) {
        this.$message.warning('请输入用户名和密码')
        return
      }

      this.loading = true

      try {
        await this.login(this.form)
        this.$message.success('登录成功!')

        // 跳转到原来要访问的页面或首页
        const redirect = this.$route.query.redirect || '/feed'
        this.$router.push(redirect)
      } catch (error) {
        console.error('登录失败', error)
        this.$message.error('登录失败: ' + (error.response?.data?.message || error.message))
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #000 0%, #333 100%);
  color: #fff;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 50%, rgba(255, 45, 146, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 107, 53, 0.1) 0%, transparent 50%);
  pointer-events: none;
}

.login-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px 30px;
  position: relative;
  z-index: 1;
}

.logo-section {
  text-align: center;
  margin-bottom: 50px;
}

.logo {
  font-size: 48px;
  font-weight: bold;
  margin-bottom: 15px;
  background: linear-gradient(135deg, #ff2d92, #ff6b35);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.tagline {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 300;
}

.form-section {
  width: 100%;
  max-width: 320px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.password-group {
  position: relative;
}

.input-field {
  width: 100%;
  padding: 15px 20px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 25px;
  color: #fff;
  font-size: 16px;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.input-field:focus {
  outline: none;
  border-color: #ff2d92;
  box-shadow: 0 0 20px rgba(255, 45, 146, 0.3);
  background: rgba(255, 255, 255, 0.15);
}

.input-field::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.password-toggle {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  font-size: 20px;
  user-select: none;
  transition: all 0.3s ease;
}

.password-toggle:hover {
  transform: translateY(-50%) scale(1.1);
}

.login-btn {
  width: 100%;
  padding: 15px 20px;
  background: linear-gradient(135deg, #ff2d92, #ff6b35);
  color: #fff;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 30px 0 20px 0;
  box-shadow: 0 4px 15px rgba(255, 45, 146, 0.3);
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(255, 45, 146, 0.4);
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.register-link {
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

.link-text {
  color: #ff2d92;
  cursor: pointer;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s ease;
}

.link-text:hover {
  color: #ff6b35;
  text-decoration: underline;
}

.footer {
  padding: 20px;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
  font-size: 12px;
  position: relative;
  z-index: 1;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .login-content {
    padding: 30px 20px;
  }

  .logo {
    font-size: 40px;
  }

  .form-section {
    max-width: 280px;
  }

  .input-field {
    padding: 12px 16px;
    font-size: 14px;
  }
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.form-section {
  animation: fadeInUp 0.6s ease-out;
}

.logo-section {
  animation: fadeInUp 0.6s ease-out 0.2s both;
}
</style>